* BUILDS TABLES IN APPENDIX

use data2025, clear

* ---> For convenience, rename & label variables here
rename bipman_ev BD
rename bdsib_ev BDSIB
rename depranx_ev DEPRANX
rename schizo_ev SCHIZO

* Note: all the variables above should be defined based on ever diagnosed + ever prescribed specific medications

rename earnings EARN
rename totalincome INC
rename disab DISAB
rename female FEMALE
rename birthcohort BIRTHCOHORT
rename year YEAR
rename momid MOMID
rename college COLLEGE 
rename highschool HS 
rename married MARRIED 
rename divorced DIVORCED 


gen MENTAL = DEPRANX == 1 | SCHIZO == 1 | BD == 1 
replace MENTAL = . if DEPRANX == . | SCHIZO == . | BD == . | ANX == .

label var MENTAL 	"MH condition"
label var BD 		"BD"
label var BDSIB 	"BD sibling"
label var DEPRANX 		"Depr/anxiety"
label var SCHIZO 	"Schizophrenia"

* "fix" or create some variables
gen LNEARN = ln(EARN)
replace LNEARN = 0 if LNEARN == .
gen NOEARN = EARN == 0
gen LNINC = ln(INC) 
replace LNINC = 0 if LNINC == .
gen NOINC = INC == 0



* Number of siblings
egen t = tag(PNR MOMID)
bysort MOMID: egen NRSIB = sum(t)
drop t

** generate wage stats within isco08 and isco88 classes

bysort isco88 YEAR: egen earnings_median88 = median(EARN)
bysort isco08 YEAR: egen earnings_median08 = median(EARN)
bysort isco88 YEAR: egen earnings_mean88 = mean(EARN)
bysort isco08 YEAR: egen earnings_mean08 = mean(EARN)

gen earnings_median = earnings_median08
replace earnings_median = earnings_median88 if earnings_median == . 
gen earnings_mean = earnings_mean08
replace earnings_mean = earnings_mean88 if earnings_mean == . 



*************************************************** DATA PREP UNTIL HERE ***************************************************



* Table A4 - No family FEs, full sample -- RERUN
eststo r1: areg NOEARN BD c.BD#c.POST FEMALE DEPRANX SCHIZO c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR, a(BIRTHCOHORT) cluster(PNR) 
sum NOEARN if e(sample)
estadd scalar ymean = r(mean)
eststo r2: areg NOEARN BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR if NOEARN == 0, a(BIRTHCOHORT) cluster(PNR) 
sum NOEARN if e(sample)
estadd scalar ymean = r(mean)
eststo r3: areg LNEARN BD c.BD#c.POST FEMALE DEPRANX SCHIZO c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR if NOEARN == 0, a(BIRTHCOHORT) cluster(PNR) 
eststo r4: areg LNEARN BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR if NOEARN == 0, a(BIRTHCOHORT) cluster(PNR) 
esttab r1 r2 r3 r4 using "tableA4.csv", b(3) se(3) keep(BD c.BD#c.POST MENTAL c.MENTAL#c.POST) stats(ymean N N_clust r2, fmt(3 0 0 2)) starlevel(* .1 ** .05 *** .01) replace


* Table A5 - No family FEs, sample w/MOMID sample -- RERUN
preserve
keep if MOMID != .
eststo r1: areg NOEARN BD c.BD#c.POST FEMALE DEPRANX SCHIZO c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR, a(BIRTHCOHORT) cluster(MOMID) 
sum NOEARN if e(sample)
estadd scalar ymean = r(mean)
eststo r2: areg NOEARN BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR , a(BIRTHCOHORT) cluster(MOMID) 
sum NOEARN if e(sample)
estadd scalar ymean = r(mean)
eststo r3: areg LNEARN BD c.BD#c.POST FEMALE DEPRANX SCHIZO c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR if NOEARN == 0, a(BIRTHCOHORT) cluster(MOMID) 
eststo r4: areg LNEARN BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR if NOEARN == 0, a(BIRTHCOHORT) cluster(MOMID) 
esttab r1 r2 r3 r4 using "tableA5.csv", b(3) se(3) keep(BD c.BD#c.POST MENTAL c.MENTAL#c.POST) stats(ymean N N_clust r2, fmt(3 0 0 2)) starlevel(* .1 ** .05 *** .01) replace
restore


* Table A6 - Retaining observations for the first 15 years a cohort is observed
preserve
bysort BIRTHCOHORT: egen minyear = min(YEAR)
bysort BIRTHCOHORT: egen maxyear = minyear + 14
keep if YEAR >= minyear & YEAR <= maxyear  
eststo r1: areg NOEARN BD c.BD#c.POST FEMALE DEPRANX SCHIZO c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
sum NOEARN if e(sample)
estadd scalar ymean = r(mean)
eststo r2: areg NOEARN BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR i.BIRTHCOHORT if NOEARN == 0, a(MOMID) cluster(MOMID) 
sum NOEARN if e(sample)
estadd scalar ymean = r(mean)
eststo r3: areg LNEARN BD c.BD#c.POST FEMALE DEPRANX SCHIZO c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR i.BIRTHCOHORT if NOEARN == 0, a(MOMID) cluster(MOMID) 
eststo r4: areg LNEARN BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR i.BIRTHCOHORT if NOEARN == 0, a(MOMID) cluster(MOMID) 
esttab r1 r2 r3 r4 using "tableA6.csv", b(3) se(3) keep(BD c.BD#c.POST MENTAL c.MENTAL#c.POST) stats(ymean N N_clust r2, fmt(3 0 0 2)) starlevel(* .1 ** .05 *** .01) replace
restore

* Table A7 - occupational sorting
local n = 1
gen lnearnings_median = ln(earnings_median)
gen lnearnings_mean = ln(earnings_mean)
foreach yvar in lnearnings_median  {
	eststo r`n': areg `yvar' c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
	local n = `n' + 1
	}

foreach yvar in management decision pressure artistic social   {
	eststo r`n': areg `yvar' c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
	sum `yvar' if e(sample)
	estadd scalar ymean = r(mean)
	local n = `n' + 1
}


eststo r`n': areg LNEARN BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.YEAR c.FEMALE#i.BIRTHCOHORT i.YEAR i.BIRTHCOHORT i.OCC i.OCC#i.YEAR if NOEARN == 0, a(MOMID) cluster(MOMID) 


esttab r1 r2 r3 r4 r5 r6 r7 r8 using "tableA7.csv", b(3) se(3) keep(BD c.BD#c.POST MENTAL c.MENTAL#c.POST) stats(ymean N N_clust r2, fmt(3 0 0 2)) starlevel(* .1 ** .05 *** .01) replace


* Table A8 - education, marriage, divorce
preserve
collapse BIRTHCOHORT BD DEPRANX SCHIZO POST FEMALE MENTAL (max) COLLEGE MARRIED DIVORCED , by(PNR MOMID)

eststo r1: areg COLLEGE BD c.BD#c.POST FEMALE DEPRANX SCHIZO c.FEMALE#i.BIRTHCOHORT i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
sum COLLEGE if e(sample)
estadd scalar ymean = r(mean)

eststo r2: areg COLLEGE BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.BIRTHCOHORT i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
sum COLLEGE if e(sample)
estadd scalar ymean = r(mean)

eststo r3: areg MARRIED BD c.BD#c.POST DEPRANX SCHIZOFEMALE c.FEMALE#i.BIRTHCOHORT i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
sum MARRIED if e(sample)
estadd scalar ymean = r(mean)

eststo r4: areg MARRIED BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.BIRTHCOHORT i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
sum MARRIED if e(sample)
estadd scalar ymean = r(mean)

eststo r5: areg DIVORCED BD c.BD#c.POST DEPRANX SCHIZO FEMALE c.FEMALE#i.BIRTHCOHORT i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
sum DIVORCED if e(sample)
estadd scalar ymean = r(mean)

eststo r6: areg DIVORCED BD c.BD#c.POST MENTAL c.MENTAL#c.POST FEMALE c.FEMALE#i.BIRTHCOHORT i.BIRTHCOHORT, a(MOMID) cluster(MOMID) 
sum DIVORCED if e(sample)
estadd scalar ymean = r(mean)

esttab r1 r2 r3 r4 r5 r6 using "tableA8.csv", b(3) se(3) keep(BD c.BD#c.POST MENTAL c.MENTAL#c.POST) stats(ymean N N_clust r2, fmt(3 0 0 2)) starlevel(* .1 ** .05 *** .01) replace
restore



